package com.itcam.tingshu.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itcam.tingshu.account.mapper.RechargeInfoMapper;
import com.itcam.tingshu.account.service.RechargeInfoService;
import com.itcam.tingshu.common.constant.SystemConstant;
import com.itcam.tingshu.model.account.RechargeInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcam.tingshu.vo.account.RechargeInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    /**
     * 根据订单号查询充值信息
     * @param orderNo
     * @return
     */
    @Override
    public RechargeInfo getRechargeInfoByOrderNo(String orderNo) {
        // 根据订单号查询对象
        return this.getOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo));
    }

    /**
     * 微信充值
     * @param rechargeInfoVo
     * @param userId
     * @return
     */
    @Override
    public String submitRecharge(RechargeInfoVo rechargeInfoVo, Long userId) {
        // 保存充值信息，添加账户余额，记录账户资金流向
        // 创建对象并赋值
        RechargeInfo rechargeInfo = new RechargeInfo();
        //  进行属性拷贝
        // BeanUtils.copyProperties(rechargeInfoVo, rechargeInfo);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfo.getPayWay());
        rechargeInfo.setUserId(userId);
        rechargeInfo.setOrderNo(UUID.randomUUID().toString().replaceAll("-", ""));
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        // 保存数据
        rechargeInfoMapper.insert(rechargeInfo);
        // 返回充值订单编号
        return rechargeInfo.getOrderNo();
    }
}
